package com.ruoyi.project.module.remark.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.common.collect.Lists;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.module.project.domain.Project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.module.remark.mapper.RemarkMapper;
import com.ruoyi.project.module.remark.domain.Remark;
import com.ruoyi.project.module.remark.service.IRemarkService;
import com.ruoyi.common.support.Convert;
import javax.annotation.Resource;

/**
 * 补全与说明 服务层实现
 * 
 * @author ruoyi
 * @date 2018-12-14
 */
@Service
public class RemarkServiceImpl implements IRemarkService 
{
	@Resource
	private RemarkMapper remarkMapper;

	/**
     * 查询补全与说明信息
     * 
     * @param completeRemarkId 补全与说明ID
     * @return 补全与说明信息
     */
    @Override
	public Remark selectRemarkById(Long completeRemarkId)
	{
	    return remarkMapper.selectRemarkById(completeRemarkId);
	}
	
	/**
     * 查询补全与说明列表
     * 
     * @param remark 补全与说明信息
     * @return 补全与说明集合
     */
	@Override
	public List<Remark> selectRemarkList(Remark remark)
	{
	    return remarkMapper.selectRemarkList(remark);
	}
	
    /**
     * 新增补全与说明
     * 
     * @param remark 补全与说明信息
     * @return 结果
     */
	@Override
	public int insertRemark(Remark remark)
	{
		String userName = ShiroUtils.getUser().getLoginName();
		remark.setCreateBy(userName);
		remark.setCreateTime(new Date());
		remark.setUpdateBy(userName);
	    return remarkMapper.insertRemark(remark);
	}
	
	/**
     * 修改补全与说明
     * 
     * @param remark 补全与说明信息
     * @return 结果
     */
	@Override
	public int updateRemark(Remark remark)
	{
		String userName = ShiroUtils.getUser().getLoginName();
		remark.setCreateBy(null);
		remark.setCreateTime(null);
		remark.setUpdateBy(userName);
	    return remarkMapper.updateRemark(remark);
	}

	/**
     * 删除补全与说明对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteRemarkByIds(String ids)
	{
		return remarkMapper.deleteRemarkByIds(Convert.toStrArray(ids));
	}


	@Override
	public int addCompleteRemark(Remark remark) {
		String groupName = remark.getGroupName();
		List<Remark> remarks = selectRemarkList(remark);
		if(remarks.size()>0){
			return remarkMapper.addApplyCount(remark);
		}else{
			return insertRemark(remark);
		}
	}

	@Override
	public List<Remark> completeRemark(Remark remark) {
		ArrayList<Remark> list = Lists.newArrayList();
		List<Remark> remarks = remarkMapper.completeRemarkList(remark);
		for (Remark remarkTemp:remarks) {
			if(!list.contains(remarkTemp)){
				list.add(remarkTemp);
			}
		}


		return list;
	}
}
